Day 21

Author

Maya McCain

library(dataRetrieval)
library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.4     ✔ readr     2.1.5
✔ forcats   1.0.0     ✔ stringr   1.5.1
✔ ggplot2   3.5.1     ✔ tibble    3.2.1
✔ lubridate 1.9.4     ✔ tidyr     1.3.1
✔ purrr     1.0.4     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(dplyr)
library(tidymodels)
── Attaching packages ────────────────────────────────────── tidymodels 1.3.0 ──
✔ broom        1.0.7     ✔ rsample      1.2.1
✔ dials        1.4.0     ✔ tune         1.3.0
✔ infer        1.0.7     ✔ workflows    1.2.0
✔ modeldata    1.4.0     ✔ workflowsets 1.1.0
✔ parsnip      1.3.0     ✔ yardstick    1.3.2
✔ recipes      1.1.1     
── Conflicts ───────────────────────────────────────── tidymodels_conflicts() ──
✖ scales::discard() masks purrr::discard()
✖ dplyr::filter()   masks stats::filter()
✖ recipes::fixed()  masks stringr::fixed()
✖ dplyr::lag()      masks stats::lag()
✖ yardstick::spec() masks readr::spec()
✖ recipes::step()   masks stats::step()
library(zoo)

Attaching package: 'zoo'

The following objects are masked from 'package:base':

    as.Date, as.Date.numeric
library(lubridate)
library(tsibble)
Registered S3 method overwritten by 'tsibble':
  method               from 
  as_tibble.grouped_df dplyr

Attaching package: 'tsibble'

The following object is masked from 'package:zoo':

    index

The following object is masked from 'package:lubridate':

    interval

The following objects are masked from 'package:base':

    intersect, setdiff, union
library(feasts)
Loading required package: fabletools

Attaching package: 'fabletools'

The following object is masked from 'package:yardstick':

    accuracy

The following object is masked from 'package:parsnip':

    null_model

The following objects are masked from 'package:infer':

    generate, hypothesize
library(plotly)

Attaching package: 'plotly'

The following object is masked from 'package:ggplot2':

    last_plot

The following object is masked from 'package:stats':

    filter

The following object is masked from 'package:graphics':

    layout
# Example: Cache la Poudre River at Mouth (USGS site 06752260)
poudre_flow <- readNWISdv(siteNumber = "06752260",    # Download data from USGS for site 06752260
                          parameterCd = "00060",      # Parameter code 00060 = discharge in cfs)
                          startDate = "2013-01-01",   # Set the start date
                          endDate = "2023-12-31") |>  # Set the end date
  renameNWISColumns() |>                              # Rename columns to standard names (e.g., "Flow", "Date")
  mutate(Date = yearmonth(Date)) |>                   # Convert daily Date values into a year-month format (e.g., "2023 Jan")
  group_by(Date) |>                                   # Group the data by the new monthly Date
  summarise(Flow = mean(Flow))                       # Calculate the average daily flow for each month
GET:https://waterservices.usgs.gov/nwis/dv/?site=06752260&format=waterml%2C1.1&ParameterCd=00060&StatCd=00003&startDT=2013-01-01&endDT=2023-12-31

Convert to a tsibble

poud_tbl <- as_tsibble(poudre_flow)
Using `Date` as index variable.
head(poud_tbl)
# A tsibble: 6 x 2 [1M]
      Date   Flow
     <mth>  <dbl>
1 2013 Jan  18.1 
2 2013 Feb  18.0 
3 2013 Mar   8.21
4 2013 Apr   5.94
5 2013 May 333.  
6 2013 Jun 300.  

Plotting the time series

flow_plot <- ggplot(poudre_flow, aes(x = Date, y = Flow)) +
  geom_line(color = "darkblue") +
  labs(title = "Cache la Poudre River Streamflow",
       x = "Date", y = "Flow (cfs)") +
  theme_minimal()
ggplotly(flow_plot)

Subseries

gg_subseries(poud_tbl) +
  labs(title = "Monthly Poudre River Streamflow Patterns", y = "Flow", x = "Year") + 
  theme_minimal()
Plot variable not specified, automatically selected `y = Flow`

I notice that streamflow is relatively low October through March consistenetly throughout the 10 year period. May and June have significantly higher streamflows than the rest of the months while April, July, August, and September have slightly higher flows than October through March. I also see that there was abnormally high streamflows in September of 2014 which may indicate a flood. Seasons are not specifically defined in this plot, however streamflow is divided up by month. We can assume that there is a season of low flows (September-March) and a season of high flows (April-August). The subseries represents how streamflows during January behaves across the ten year period. This allows us to compare how the months behave over the years.

Decompose

poud_decomp <- stl(poud_tbl, s.window = "periodic") |>
  plot()

The seasonal component is most appropriate for Poudre River streamflow data. The plot shows various patterns in flows over a ten year period. The seasonal window depicts streamflow patterns throughout the months of the year, most likely related to snowmelt patterns. Seasonal streamflow has stayed relatively the same in the last ten years. The trend window depicts streamflow levels over the ten year period, most likely related to long term influences such as climate change. Trend streamflows have decreased in the last ten years.